/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.auzingers.docsorganizer.googleapi;

/**
 * Utility class for Google references.
 */
public final class GoogleRefUtil {

    private static final String TYPE_SEPARATOR = "%3A";

    /** Private constructor of utility class. */
    private GoogleRefUtil() {
        //private constructor
    }

    /**
     * Returns only the part after the last "/" of the given link - and removes the doctype as well.
     * I.e. if the reference is
     * "https://docs.google.com/feeds/id/spreadsheet%3A0AkoZ3CeNrlNMdFRHa2s2ZVpmNTczMndUWUdFeThyNHc" only
     * "0AkoZ3CeNrlNMdFRHa2s2ZVpmNTczMndUWUdFeThyNHc" is returned.
     * @param ref reference to process
     * @return see above
     */
    public static String stripRef(String ref) {
        String result;
        if (ref == null) {
            result = null;
        } else {
            final int idx = ref.lastIndexOf('/');
            if (idx == 0) {
                result = ref;
            } else {
                result = ref.substring(idx + 1);
            }
            final int idxt = result.indexOf(TYPE_SEPARATOR);
            if (idxt > 0) {
                result = result.substring(idxt + TYPE_SEPARATOR.length());
            }
        }
        return result;
    }
}
